﻿<%@ WebHandler Language="C#" Class="system" %>

using System;
using System.Web;
using System.Data;
using NS.BLL.Systems;
using NS.Utilities;

public class system : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        //取得处事类型
        string action = context.Request["action"];

        switch (action)
        {
            case "get_menu_list": //验证扩展字段是否重复
                get_menu_list(context);
                break;
            case "checkUser": //验证用户账号是否重复
                checkUser(context);
                break;
        }
    }

    private void checkUser(HttpContext context)
    {
        string uName = context.Request["uname"];
        string uID = "0";
        if (context.Request["uid"] != null)
        {
            uID = context.Request["uid"];
        }
        string result = string.Empty;
        if (UserDetailService.IsExistUser(uName, uID))
        {
            result = JsonMessager.Fail("已被注册");
        }
        else
        {
            result = JsonMessager.Success("可用");
        }
        context.Response.Write(result);
    }

    #region 获取后台导航字符串==============================
    private void get_menu_list(HttpContext context)
    {
        DataTable dt = MenuService.GetUserMenu(CookieHelper.GetValue("uid"));
        context.Response.Write("<div class=\"list-group\" name=\"菜单\">\n");
        this.get_menu_childs(context, dt, 0);
        context.Response.Write("</div>\n");
    }
    private void get_menu_childs(HttpContext context, DataTable oldData, int parent_id)
    {
        DataRow[] dr = oldData.Select("ParentID=" + parent_id, "OrderID asc");
        for (int i = 0; i < dr.Length; i++)
        {
            if (i == 0 && parent_id > 0)
            {
                context.Response.Write("<ul>\n");
            }

            if (int.Parse(dr[i]["Depth"].ToString()) == 0)
            {
                context.Response.Write("<h2>" + dr[i]["MenuName"].ToString() + "<i></i></h2>\n");
                //调用自身迭代
                this.get_menu_childs(context, oldData, int.Parse(dr[i]["MenuID"].ToString()));
            }
            else
            {
                context.Response.Write("<li>\n");
                context.Response.Write("<a navid=\"" + dr[i]["MenuID"].ToString() + "\"");
                if (!string.IsNullOrEmpty(dr[i]["Menulink"].ToString()))
                {
                    context.Response.Write(" href=\"" + dr[i]["Menulink"].ToString() + "?mid=" + dr[i]["MenuID"].ToString() + "\" target=\"mainframe\"");
                }
                context.Response.Write(" class=\"item\">\n");
                context.Response.Write("<span>" + dr[i]["MenuName"].ToString() + "</span>\n");
                context.Response.Write("</a>\n");
                //调用自身迭代
                this.get_menu_childs(context, oldData, int.Parse(dr[i]["MenuID"].ToString()));
                context.Response.Write("</li>\n");
            }
            //以上是输出选项内容=======================
            //输出结束标记
            if (i == (dr.Length - 1) && parent_id > 0)
            {
                context.Response.Write("</ul>\n");
            }
        }

    }
    #endregion

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}